<?php
//首页控制器
class IndexControl extends IncControl{
	//展示首页
	function index(){
		// print_const();
		$order=$this->getOrder();
		// p($order);
		$model = K('article');
		$page=new Page($model->where('huodong<>1 '.$order[1])->count());
		$art = getTipStyle($model->limit($page->limit())->where('huodong<>1 '.$order[1])->order($order[0])->all()); //文章
		$slide=M('article')->where('slide=1')->order('aid DESC')->limit(7)->getField('aid,big,title');
		$this->assign('art',$art); //分配文章
		$this->assign('slide',$slide); //分配轮播图
		$this->assign('page',$page->show(3));
		$this->display('index.html');
	}
	//获取oder
	function getOrder(){
		if(isset($_GET['order'])){
			$order=Q('get.order');
			switch ($order) {
				case '1': //推荐
					return array("ldf_article.aid DESC"," AND flag=4 ");
				case '2': //热门
					return array("ldf_article.hits DESC"," AND ldf_article.hits>10 ");
				case '3': //五星
					return array("ldf_article.aid DESC"," AND star=5 ");
				case '4': //随便看看
					return array('ldf_article.aid DESC','');
			}
		}else{
			return array('ldf_article.aid DESC','');
		}
	}
	//展示文章详细内容
	function content(){
		$aid = Q('aid',0,'intval');
		$cid = Q('cid',0,'intval');
		//关联用户表
		$Cmodel = V('comment');
		$Cmodel->view=array(
			'user'=>array(
				'type'=>INNER_JOIN,
				'on'=>'comment.uid=user.uid'
			)
		);
		if(IS_POST){ //ajax显示评论
			sleep(1); //模拟网络延迟加载等待效果
			$total=M('comment')->where(array('aid'=>$aid))->count();
			$page = new page($total,2);
			$this->comment=$Cmodel->where(array('aid'=>$aid))->limit($page->limit())->order('cid DESC')->all();
			$path = ROOT_PATH.'data/Ajax/commentAjax.html';
			$this->page=$page->show();
			$data = $this->fetch($path);
			echo $data;die;
		}else{ //正常读取栏目内容
			//栏目hits+1
			M('category')->inc('hits','cid='.$cid,1);
			//文章hits+1
			M('article')->inc('hits','aid='.$aid,1);
			$model = K('article');
			$art = getTipStyle($model->field('*,ldf_article.pic')->where(array('ldf_article.aid'=>$aid))->find(),1); //文章
			$this->field=$art;
			$total=M('comment')->where(array('aid'=>$art['aid']))->count();
			$page = new page($total,2);
			$this->comment=$Cmodel->where(array('aid'=>$art['aid']))->limit($page->limit())->order('cid DESC')->all();
			$this->commentTotal=$total;
			$this->page=$page->show();
			//查询文章喜欢数
			$en = M('enjoy')->where(array('aid'=>$art['aid']))->find()?1:0;
			$this->enjoy=$en;
			// p($art);
			$this->display('content.html');
		}
	}



	//活动
	function huodong(){
		$cid=Q('cid',0,'intval');
		$page=new Page(K('article')->where(array('ldf_article.cid'=>$cid))->order('ldf_article.aid DESC')->count());
		$art = getTipStyle(K('article')->limit($page->limit())->where(array('ldf_article.cid'=>$cid))->order('ldf_article.aid DESC')->all()); //查询所有活动

		$slide = M('article')->order('aid ASC')->where(array('cid'=>$cid,'huodong=1','big<>""'))->all();// 查询活动幻灯片

		// p($art);
		// p($slide);
		$this->art=$art;
		$this->slide=$slide;
		$this->page=$page->show();
		$this->display('huodong.html');
	}


	
}